package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.entity.News;
import com.example.domain.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface NewsMapper extends BaseMapper<News> {
    //
    @Select("select * from news order by click desc")
    public List<News> recommendMaxClick();
    @Select("select * from news where newsTag=#{tag} order by click desc")
    public List<News> recommendByTag(@Param("tag") String tag);
    @Select("select * from news order by (news.praise/news.click)")
    public List<News> recommendBylikePercent();
    @Select("SELECT id, newstag, click, praise, \n" +
            "  IF(newstag = #{tag}, (0.3 + (click/20000)*0.4 + (praise/click)*0.3), ((click/20000)*0.4 + (praise/click)*0.3)) AS a\n" +
            "FROM news\n" +
            "ORDER BY a DESC;\n")
    public int[] recommendByAll(@Param("tag") String tag);
    @Select("select * from news where id=#{id}")
    public News findById(@Param("id") int id);

    @Select("SELECT n.newsTag AS Tag, \n" +
            "\t\t\t\tCOUNT(*) AS NewsCount,\n" +
            "       ROUND(AVG(IFNULL(n.praise, 0) / IFNULL(n.click, 1)), 4) AS like_rate,\n" +
            "       ROUND(AVG(IFNULL(n.click, 0)), 4) AS click_count,\n" +
            "       ROUND(AVG(IFNULL(n.finishPercent, 0)), 4) AS read_complete_rate\n" +
            "FROM news n\n" +
            "GROUP BY n.newsTag;")
    List<Map<String, Object>> selectAll();
}
